package sv.ues.fia.adminperfildocentes;

import sv.ues.fia.adminperfildocentes.model.Empresa;
import sv.ues.fia.adminperfildocentes.model.Pais;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class ConsultarEmpresaActivity extends Activity {

	ControlDB helper;
	EditText editIdEmpresa;
	EditText editPais;
	EditText editDenominacion;
	EditText editRubro;
	EditText editEmail;
	EditText editTelefono;
	EditText editDireccion;
	String id;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		helper = new ControlDB(this);
		setContentView(R.layout.activity_consultar_empresa);
		editIdEmpresa = (EditText) findViewById(R.id.editIdEmpresa);
		editPais = (EditText) findViewById(R.id.editPais);
		editDenominacion = (EditText) findViewById(R.id.editDenominacion);
		editRubro = (EditText) findViewById(R.id.editRubro);
		editEmail = (EditText) findViewById(R.id.editEmail);
		editTelefono = (EditText) findViewById(R.id.editTelefono);
		editDireccion = (EditText) findViewById(R.id.editDireccion);
		
		Bundle bundle = getIntent().getExtras();
		id = String.valueOf(bundle.getLong("idEmpresa"));
				
		helper.abrir();
		Empresa empresa = helper.consultarEmpresa(id);
		Pais pais = helper.consultarPais(String.valueOf(empresa.getIdpais()));
		helper.cerrar();
		
		if(empresa == null || pais == null)
		{
			Toast.makeText(this, getResources().getString(R.string.empresa_con) +
					id + getResources().getString(R.string.no_encontrado), Toast.LENGTH_LONG).show();
		}
		else
		{
			editIdEmpresa.setText(String.valueOf(empresa.getIdempresa()));
			editPais.setText(pais.getNombre());
			editDenominacion.setText(empresa.getDenominacion());
			editRubro.setText(empresa.getRubro());
			editEmail.setText(empresa.getEmail());
			editTelefono.setText(empresa.getTelefono());
			editDireccion.setText(empresa.getDireccion());
			id =String.valueOf(empresa.getIdempresa());
		}
	}
	
	public void llamarActivityActualizar(View v)
	{
		Intent intent = new Intent(getApplicationContext(), ActualizarEmpresaActivity.class);
		intent.putExtra("idEmpresa", id);
		startActivity(intent); 
	}
	
	public void eliminarEmpresa(View v)
	{
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
				this);
 
			// set title
			alertDialogBuilder.setTitle(R.string.eliminareg);
 
			// set dialog message
			alertDialogBuilder
				.setMessage(R.string.msjeliminar)
				.setCancelable(false)
				.setPositiveButton(R.string.si,new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int idOption) {
						
						Empresa empresa = new Empresa();
						empresa.setIdempresa(Integer.parseInt(id));
						helper.abrir();
						int accion = helper.eliminarEmpresa(empresa);
						helper.cerrar();
						if (accion == -1 || accion == 0) {
							Toast.makeText(getBaseContext(), R.string.erroreliminar, Toast.LENGTH_SHORT).show();
							Intent intent1 = new Intent(getApplicationContext(), ListarEmpresasActivity.class);
							startActivity(intent1);
						} else {
							Toast.makeText(getBaseContext(), R.string.okeliminar, Toast.LENGTH_SHORT).show();
							Intent intent2 = new Intent(getApplicationContext(), ListarEmpresasActivity.class);
							startActivity(intent2);
						}
					
						
					}
				  })
				.setNegativeButton(R.string.no,new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int id) {
						// if this button is clicked, just close
						// the dialog box and do nothing
						dialog.cancel();
					}
				});
 
				// create alert dialog
				AlertDialog alertDialog = alertDialogBuilder.create();
 
				// show it
				alertDialog.show();
	}
}
